﻿function bullet_update() {
    player_bullet_update();
    player_bullet_collide();
    enemy_bullet_update();
    enemy_bullet_collide();
}

function bullet_draw() {
    player_bullet_draw();
    enemy_bullet_draw();
}

function enemy_bullet_update() {
    var i = enemy_bullet.length;
    while (i--) {
        enemy_bullet[i].x += enemy_bullet[i].v * cos(enemy_bullet[i].ang);
        if (enemy_bullet[i].x < -20 || enemy_bullet[i].x >= WIDTH + 20) {
            enemy_bullet_remove(i);
            continue;
        }
        
        enemy_bullet[i].y += enemy_bullet[i].v * sin(enemy_bullet[i].ang);
        if (enemy_bullet[i].y < -20 || enemy_bullet[i].y >= HEIGHT + 20) {
            enemy_bullet_remove(i);
            continue;
        }
    }
}

function enemy_bullet_collide() {
    var i = enemy_bullet.length;
    while (i--) {
        var r = enemy_bullet[i].collide();
        if (r == 0) {
            // pass
        } else if (r == 1) {
            effect_2(effect_2(enemy_bullet[i].x, enemy_bullet[i].y));
        } else if (r == 2) {
            player_die();
            enemy_bullet_remove(i);
        }
    }
}

function enemy_bullet_draw() {
    var i = enemy_bullet.length;
    while (i--) {
        enemy_bullet[i].draw();
    }
}

function enemy_bullet_remove(i) {
    enemy_bullet.splice(i, 1);
}

function circle_collide() {
    var xx = abs(this.x - player.x);
    var yy = abs(this.y - player.y);
    if (xx > this.r2 || yy > this.r2)
        return 0;
        
    var s = xx * xx + yy * yy;
    if (s < this.r1 * this.r1) {
        player_die();
        return 2;
    } else if (!this.graze && s < this.r2 * this.r2) {
        this.graze = true;
        graze++;
        return 1
    }
    return 0;
}

function rect_collide() {
    var xp = player.x - this.x;
    var yp = player.y - this.y;
    var ang = this.ang - pi * 0.5;
    var xx = abs(xp * cos(ang) + yp * sin(ang));
    var yy = abs(-xp * sin(ang) + yp * cos(ang));
    
    if (xx > this.w2 || yy > this.h2)
        return 0;
        
    if (xx < this.w1 && yy < this.h1) {
        player_die();
        return 2;
    } else if (!this.graze) {
        this.graze = true;
        graze++;
        return 1;
    }
    return 0;
}

function enemy_bullet_0(bx, by, bv, bang) {
    enemy_bullet[enemy_bullet.length] = {
            x: bx, y: by, v:bv, ang: bang,
            draw: bullet_draw_0,
            graze: false,
            collide: circle_collide, r1: 4, r2: 10};
}

function enemy_bullet_1(bx, by, bv, bang) {
    enemy_bullet[enemy_bullet.length] = {
        x: bx, y: by, v:bv, ang: bang,
        draw: bullet_draw_1, 
        graze: false, 
        collide: circle_collide, r1: 3, r2: 10};
}

function enemy_bullet_2(bx, by, bv, bang) {
    enemy_bullet[enemy_bullet.length] = {
        x: bx, y: by, v:bv, ang: bang,
        draw: bullet_draw_2, 
        graze: false, 
        collide: circle_collide, r1: 4, r2: 16};
}

function enemy_bullet_3(bx, by, bv, bang) {
    enemy_bullet[enemy_bullet.length] = {
        x: bx, y: by, v:bv, ang: bang,
        draw: bullet_draw_3,
        graze: false, 
        collide: rect_collide, w1: 5, h1: 6, w2: 10, h2: 12};
}

